package cloud.keveri.springboot.starter.gateway.client;

import cloud.keveri.gateway.dto.KeveriClientConfigure;
import cloud.keveri.gateway.sdk.ClientRegisterRepositoryFactory;
import cloud.keveri.gateway.sdk.GatewayServiceDiscovery;
import cloud.keveri.registry.api.InstanceRegisterRepository;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static cloud.keveri.gateway.sdk.def.Const.Gateway_Client_Prefix;

/**
 * <p>
 * 作者：沈自在
 **/
@Configuration
@ConditionalOnProperty(value = Gateway_Client_Prefix + ".enabled", matchIfMissing = true, havingValue = "true")
public class GatewayClientAutoConfiguration {

    /**
     *
     */
    @Bean
    @ConfigurationProperties(prefix = Gateway_Client_Prefix)
    public KeveriClientConfigure keveriClientConfigure() {
        return new KeveriClientConfigure();
    }

    /**
     *
     */
    @Bean
    public InstanceRegisterRepository instanceRegisterRepository(final KeveriClientConfigure config) {
        return ClientRegisterRepositoryFactory.newInstance(config);
    }

    /**
     *
     */
    @Bean
    public GatewayServiceDiscovery gatewayServiceDiscovery(final KeveriClientConfigure configure,
                                                           final InstanceRegisterRepository register) {
        return new GatewayServiceDiscovery(configure, register);
    }
}
